package com.study.service.FDservice;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.FDjdao.FinanceDao;
import com.study.model.jdao.FDjdao.PurchasePaymentDao;
import com.study.model.pojo.FDpojo.Finance;
import com.study.model.pojo.FDpojo.PurchasePayment;
import com.study.model.pojo.PSIpojo.PurchaseOrder;
import com.study.service.PSIservice.PurchaseOrderServices;




@Service
public class PurchasePaymentServices {

	@Autowired
	PurchasePaymentDao ppd;
	@Autowired
	PurchaseOrderServices poServices;
	@Autowired
	FinanceDao fdao;
	/**
	 * 查询所有采购付款单
	 * @return
	 */
	public List<PurchasePayment> selectAllpp(){	
		return (List<PurchasePayment>) ppd.findAll();
	}
	/**
	 * 新增采购付款表记录
	 * @param id
	 */
	public void addPuchasePayment(PurchaseOrder po) {
		//新增采购付款表记录
		PurchasePayment payment =new PurchasePayment();
		//采购编号
		payment.setPurorderid(po.getPurorderid());
		//供应商
		payment.setSupplier(po.getSuppid());
		//付款时间
		payment.setPayDate(new Date());
		//需付总金额
		payment.setMoney(po.getTotalprice());
		//已付金额 
		payment.setPaidAmount(po.getNeedpay());
		//待付款
		payment.setArrearages(po.getTotalprice()-po.getNeedpay());
		//本次付款
		payment.setPayment(po.getNeedpay());
		//制单人
		payment.setPrepareder("123");
		//审核人
		payment.setVerifier("123");
		//状态
		payment.setState(0);
		//备注
		payment.setRemarks("1111");
		//执行采购付款单记录新增
		ppd.save(payment);
	}
	/**
	 * 新增财务表记录
	 * @param po
	 */
	public void addFinacne(PurchaseOrder po) {
		//新增财务表信息
		Finance finance =new Finance();
		//采购编号
		finance.setDocumentsId(po.getPurorderid());
		//单据类型
		finance.setType(2);
		//金额
		finance.setMoney(po.getNeedpay());
		//日期时间
		finance.setDate(new Date());
		//备注信息
		finance.setRemark("123");
		//执行财务表记录新增操作
		fdao.save(finance);			
	}
	/**
	  *  采购表审核
	 * @param id
	 * @return
	 */
	public Boolean audit(String id,int state,String remarks) {
		System.out.println("123!!!!!!!!!!!!");
		try {		
			//根据id查询采购单信息			
			PurchaseOrder po =  poServices.findByPurorderid(id);
			
			System.out.println("次采购订单编号：【"+po.getPurorderid()+"】");
			//修改状态
			po.setFinancestate(state);
			if(remarks != null) {
				po.setRemark(remarks);
			}
			//保存
			poServices.savePurchaseOrder(po);
			//调用采购付款表记录新增方法
			this.addPuchasePayment(po);
			System.out.println("新增付款记录");
			//调用财务记录新增方法
			this.addFinacne(po);
			System.out.println("新增财务记录");
			return true;
		} catch (Exception e) {
			System.out.println("审核失败："+e);
		}
		return false;
	}	
}
